from flask import Flask, request, jsonify, redirect
from flask import Flask, send_from_directory, render_template
from flask_cors import CORS
import requests
app = Flask(__name__)
CORS(app)
CORS(app, resources={r"/singer/*": {"origins": "http://localhost:63342"}})
CORS(app, supports_credentials=True)

# 首页，测试服务器运行状态
@app.route('/')
def index():
    return render_template('shouye.html')

@app.route('/result')
def result():
    return render_template('result.html')

@app.route('/singer_page')
def singer_page():
    return render_template('singer_list.html')

@app.route('/singer_detail')
def singer_detail():
    singer_id = request.args.get('singer_id')
    # 可以在这里添加获取歌手详细信息的逻辑
    return render_template('singer_detail.html', singer_id=singer_id)

@app.route('/playlist_detail')
def playlist_detail():
    playlist_id = request.args.get('playlist_id')
    return render_template('playlist_detail.html', playlist_id=playlist_id)


@app.route('/user/refresh', methods=['GET'])
def user_refresh():
    response = requests.get('http://121.40.228.127:3300/user/refresh')
    return jsonify(response.json())


@app.route('/song/urls', methods=['GET'])
def song_urls():
    song_ids = request.args.get('id')
    if not song_ids:
        return jsonify({'error': 'No song ID provided'}), 400

    # 刷新用户登录状态
    refresh_response = requests.get('http://121.40.228.127:3300/user/refresh')
    refresh_data = refresh_response.json()
    if refresh_response.status_code != 200 or refresh_data.get('result') != 100:
        return jsonify({'error': 'Failed to refresh user login', 'details': refresh_data.get('errMsg')}), 400

    # 获取存储的Cookie
    cookie_response = requests.get(f'http://121.40.228.127:3300/user/getCookie?id=969359586')
    cookie_data = cookie_response.json()
    if cookie_response.status_code != 200 or cookie_data.get('result') != 100:
        return jsonify({'error': 'Failed to get cookies', 'details': cookie_data.get('errMsg')}), 400

    cookies = cookie_data.get('data')

    print(f"Retrieved cookies: {cookies}")  # 打印Cookies以进行调试

    headers = {
        'Cookie': '; '.join([f"{key}={value}" for key, value in cookies.items()])
    }

    response = requests.get(f'http://121.40.228.127:3300/song/urls?id={song_ids}', headers=headers)
    response.headers['Access-Control-Allow-Origin'] = 'http://localhost:63342'
    response.headers['Access-Control-Allow-Credentials'] = 'true'
    return jsonify(response.json())


@app.route('/song/url', methods=['GET'])
def get_song_url():
    songmid = request.args.get('id')
    type = request.args.get('type', '128')
    mediaId = request.args.get('mediaId', songmid)

    # 检查必要参数
    if not songmid:
        return jsonify({'error': '缺少必要参数: id'}), 400

    # 构建API请求URL
    url = f"http://121.40.228.127:3300/song/url?id={songmid}&type={type}&mediaId={mediaId}"

    # 发送请求到实际的API接口
    response = requests.get(url, cookies={'cookie': 'qq_domain_video_guid_verify=0572ccfe8fb71d4c; pgv_pvid=7514459580; _qimei_q36=; _qimei_h38=fded156bd15c06d8362ab92c02000005417b17; RK=gF39lAty7k; ptcz=4e0c227ec1180b8dc77feddbf4865128159cc539108aa1b235bbd21143d60dd4; fqm_pvqid=760c9673-a3b4-42ad-9ca2-7876a16dc944; ts_uid=376202578; tmeLoginType=2; music_ignore_pskey=202306271436Hn@vBj; ptui_loginuin=1243646228; o_cookie=1243646228; euin=NKCqoi4q7Kcs; fqm_sessionid=23a82fb3-7c71-4156-8a1d-da6405adbbfc; pgv_info=ssid=s2719169404; ts_last=y.qq.com/; ts_refer=www.bing.com/; _qpsvr_localtk=0.48274172097522405; login_type=1; uin=969359586; wxrefresh_token=; psrf_qqunionid=B47A35EE4695201B6E233283FA90AC2F; psrf_qqaccess_token=A44A487B421913EF5438544A69B125DB; wxunionid=; psrf_qqopenid=BA2251B72AB9F5CF7DFA3AD7FDC96ED2; psrf_qqrefresh_token=8448FC9E47D30B780F6DE8508FE49EDA; psrf_musickey_createtime=1719158917; wxopenid=; qm_keyst=Q_H_L_63k3NPtDOB5775HYBfLk_O-6bXLrKVDtD2hO-36Y8YRERbZ0Tu7Wh91ObFzSTjP_g7FhsZe5HDg; psrf_access_token_expiresAt=1726934916; qqmusic_key=Q_H_L_63k3NPtDOB5775HYBfLk_O-6bXLrKVDtD2hO-36Y8YRERbZ0Tu7Wh91ObFzSTjP_g7FhsZe5HDg'})

    if response.status_code == 200:
        data = response.json()
        return jsonify(data)
    else:
        return jsonify({'error': '请求失败'}), response.status_code

@app.route('/search', methods=['GET'])
def search():
    keyword = request.args.get('key')
    page_no = request.args.get('pageNo', 1)
    page_size = request.args.get('pageSize', 20)
    search_type = request.args.get('t', 0)

    if not keyword:
        return jsonify({'error': 'No keyword provided'}), 400

    # 发送请求到你的服务器
    response = requests.get(f'http://121.40.228.127:3300/search', params={
        'key': keyword, 'pageNo': page_no, 'pageSize': page_size, 't': search_type
    })
    return jsonify(response.json())


@app.route('/user/setCookie', methods=['POST'])
def set_cookie():
    cookie_data = request.json.get('data')
    if not cookie_data:
        return jsonify({'error': 'No cookie data provided'}), 400

    response = requests.post('http://121.40.228.127:3300/user/setCookie', json={'data': cookie_data})
    return jsonify(response.json())



@app.route('/user/getCookie', methods=['GET'])
def get_cookie():
    user_id = request.args.get('id')
    if not user_id:
        return jsonify({'error': 'No user ID provided'}), 400

    response = requests.get(f'http://121.40.228.127:3300/user/getCookie?id={user_id}')
    print("Get Cookie response status code:", response.status_code)
    print("Get Cookie response content:", response.content)

    return jsonify(response.json())


@app.route('/search/hot', methods=['GET'])
def hot_search():
    # 发送请求到你的服务器获取热搜词
    response = requests.get('http://121.40.228.127:3300/search/hot')
    return jsonify(response.json())  # 返回从你的API获取的数据


@app.route('/search/quick', methods=['GET'])
def quick_search():
    keyword = request.args.get('key')
    if not keyword:
        return jsonify({'error': 'No keyword provided'}), 400

    # 发送请求到你的服务器进行快速搜索
    response = requests.get(f'http://121.40.228.127:3300/search/quick', params={'key': keyword})
    return jsonify(response.json())


@app.route('/user/cookie', methods=['GET'])
def user_cookie():
    response = requests.get('http://121.40.228.127:3300/user/cookie')
    return jsonify(response.json())



@app.route('/user/detail', methods=['GET'])
def user_detail():
    user_id = request.args.get('id')
    if not user_id:
        return jsonify({'error': 'No user ID provided'}), 400
    response = requests.get(f'http://121.40.228.127:3300/user/detail?id={user_id}')
    return jsonify(response.json())

@app.route('/user/songlist', methods=['GET'])
def user_songlist():
    user_id = request.args.get('id')
    if not user_id:
        return jsonify({'error': 'No user ID provided'}), 400
    response = requests.get(f'http://121.40.228.127:3300/user/songlist?id={user_id}')
    return jsonify(response.json())

@app.route('/user/collect/songlist', methods=['GET'])
def user_collect_songlist():
    user_id = request.args.get('id')
    page_no = request.args.get('pageNo', 1)
    page_size = request.args.get('pageSize', 20)
    response = requests.get(f'http://121.40.228.127:3300/user/collect/songlist', params={'id': user_id, 'pageNo': page_no, 'pageSize': page_size})
    return jsonify(response.json())

@app.route('/user/collect/album', methods=['GET'])
def user_collect_album():
    user_id = request.args.get('id')
    page_no = request.args.get('pageNo', 1)
    page_size = request.args.get('pageSize', 20)
    response = requests.get(f'http://121.40.228.127:3300/user/collect/album', params={'id': user_id, 'pageNo': page_no, 'pageSize': page_size})
    return jsonify(response.json())

@app.route('/songlist', methods=['GET'])
def get_songlist_details():
    songlist_id = request.args.get('id')
    if not songlist_id:
        return jsonify({'error': 'No songlist ID provided'}), 400
    response = requests.get(f'http://121.40.228.127:3300/songlist?id={songlist_id}')
    return jsonify(response.json())

@app.route('/songlist/category', methods=['GET'])
def get_songlist_categories():
    response = requests.get('http://121.40.228.127:3300/songlist/category')
    return jsonify(response.json())

@app.route('/songlist/list', methods=['GET'])
def get_songlists_by_category():
    category_id = request.args.get('category', '10000000')
    page_no = request.args.get('pageNo', '1')
    page_size = request.args.get('pageSize', '20')
    sort = request.args.get('sort', '5')
    response = requests.get(f'http://121.40.228.127:3300/songlist/list', params={'category': category_id, 'pageNo': page_no, 'pageSize': page_size, 'sort': sort})
    return jsonify(response.json())


@app.route('/songlist/map', methods=['GET'])
def songlist_map():
    dirid = request.args.get('dirid', '201')
    response = requests.get(f'http://121.40.228.127:3300/songlist/map', params={'dirid': dirid})
    return jsonify(response.json())

@app.route('/songlist/add', methods=['POST'])
def add_song_to_songlist():
    mid = request.json.get('mid')
    dirid = request.json.get('dirid')
    response = requests.post('http://121.40.228.127:3300/songlist/add', json={'mid': mid, 'dirid': dirid})
    return jsonify(response.json())

@app.route('/songlist/remove', methods=['POST'])
def remove_song_from_songlist():
    song_id = request.json.get('id')
    dirid = request.json.get('dirid')
    response = requests.post('http://121.40.228.127:3300/songlist/remove', json={'id': song_id, 'dirid': dirid})
    return jsonify(response.json())

@app.route('/songlist/create', methods=['POST'])
def create_songlist():
    name = request.json.get('name')
    response = requests.post('http://121.40.228.127:3300/songlist/create', json={'name': name})
    return jsonify(response.json())

@app.route('/songlist/delete', methods=['POST'])
def delete_songlist():
    dirid = request.json.get('dirid')
    response = requests.post('http://121.40.228.127:3300/songlist/delete', json={'dirid': dirid})
    return jsonify(response.json())

@app.route('/songlist/collect', methods=['POST'])
def collect_songlist():
    songlist_id = request.json.get('id')
    op = request.json.get('op')
    response = requests.post('http://121.40.228.127:3300/songlist/collect', json={'id': songlist_id, 'op': op})
    return jsonify(response.json())

@app.route('/song', methods=['GET'])
def get_song():
    songmid = request.args.get('songmid')
    if not songmid:
        return jsonify({'error': 'No songmid provided'}), 400
    response = requests.get(f'http://121.40.228.127:3300/song?songmid={songmid}')
    return jsonify(response.json())

@app.route('/song/similar', methods=['GET'])
def get_similar_songs():
    song_id = request.args.get('id')
    if not song_id:
        return jsonify({'error': 'No song ID provided'}), 400
    response = requests.get(f'http://121.40.228.127:3300/song/similar?id={song_id}')
    return jsonify(response.json())

@app.route('/song/playlist', methods=['GET'])
def get_related_playlist():
    song_id = request.args.get('id')
    if not song_id:
        return jsonify({'error': 'No song ID provided'}), 400
    response = requests.get(f'http://121.40.228.127:3300/song/playlist?id={song_id}')
    return jsonify(response.json())

@app.route('/song/mv', methods=['GET'])
def get_related_mv():
    song_id = request.args.get('id')
    if not song_id:
        return jsonify({'error': 'No song ID provided'}), 400
    response = requests.get(f'http://121.40.228.127:3300/song/mv?id={song_id}')
    return jsonify(response.json())


@app.route('/lyric', methods=['GET'])
def get_lyrics():
    songmid = request.args.get('songmid')
    if not songmid:
        return jsonify({'error': 'No songmid provided'}), 400
    response = requests.get(f'http://121.40.228.127:3300/lyric?songmid={songmid}')
    return jsonify(response.json())


@app.route('/recommend/playlist/u', methods=['GET'])
def recommend_playlist_for_you():
    response = requests.get('http://121.40.228.127:3300/recommend/playlist/u')
    return jsonify(response.json())


@app.route('/recommend/playlist', methods=['GET'])
def recommend_playlist_by_category():
    category_id = request.args.get('id', '3317')
    page_no = request.args.get('pageNo', '1')
    page_size = request.args.get('pageSize', '20')
    response = requests.get(f'http://121.40.228.127:3300/recommend/playlist', params={'id': category_id, 'pageNo': page_no, 'pageSize': page_size})
    return jsonify(response.json())

@app.route('/recommend/daily', methods=['GET'])
def daily_recommendations():
    response = requests.get('http://121.40.228.127:3300/recommend/daily')
    return jsonify(response.json())


@app.route('/recommend/banner', methods=['GET'])
def get_banner():
    response = requests.get('http://121.40.228.127:3300/recommend/banner')
    return jsonify(response.json())



@app.route('/new/songs', methods=['GET'])
def new_songs():
    music_type = request.args.get('type', '0')
    response = requests.get(f'http://121.40.228.127:3300/new/songs', params={'type': music_type})
    return jsonify(response.json())

@app.route('/new/album', methods=['GET'])
def new_album():
    music_type = request.args.get('type', '1')
    num = request.args.get('num', '10')
    response = requests.get(f'http://121.40.228.127:3300/new/album', params={'type': music_type, 'num': num})
    return jsonify(response.json())

@app.route('/new/mv', methods=['GET'])
def new_mv():
    mv_type = request.args.get('type', '0')
    response = requests.get(f'http://121.40.228.127:3300/new/mv', params={'type': mv_type})
    return jsonify(response.json())

@app.route('/singer/desc', methods=['GET'])
def singer_desc():
    singermid = request.args.get('singermid')
    if not singermid:
        return jsonify({'error': 'No singermid provided'}), 400
    response = requests.get(f'http://121.40.228.127:3300/singer/desc?singermid={singermid}')
    return jsonify(response.json())

@app.route('/singer/songs', methods=['GET'])
def singer_songs():
    singermid = request.args.get('singermid')
    num = request.args.get('num', '20')
    page = request.args.get('page', '1')
    response = requests.get(f'http://121.40.228.127:3300/singer/songs?singermid={singermid}&page={page}&num={num}')
    return jsonify(response.json())

@app.route('/singer/album', methods=['GET'])
def singer_album():
    singermid = request.args.get('singermid')
    pageNo = request.args.get('pageNo', '1')
    pageSize = request.args.get('pageSize', '20')
    response = requests.get(f'http://121.40.228.127:3300/singer/album?singermid={singermid}&pageNo={pageNo}&pageSize={pageSize}')
    return jsonify(response.json())

@app.route('/singer/mv', methods=['GET'])
def singer_mv():
    singermid = request.args.get('singermid')
    pageNo = request.args.get('pageNo', '1')
    pageSize = request.args.get('pageSize', '20')
    response = requests.get(f'http://121.40.228.127:3300/singer/mv?singermid={singermid}&pageNo={pageNo}&pageSize={pageSize}')
    return jsonify(response.json())
@app.route('/singer/sim', methods=['GET'])
def singer_similar():
    singermid = request.args.get('singermid')
    response = requests.get(f'http://121.40.228.127:3300/singer/sim?singermid={singermid}')
    return jsonify(response.json())

@app.route('/singer/category', methods=['GET'])
def singer_category():
    response = requests.get('http://121.40.228.127:3300/singer/category')
    return jsonify(response.json())

@app.route('/singer/list', methods=['GET'])
def singer_list():
    area = request.args.get('area', '-100')
    genre = request.args.get('genre', '-100')
    index = request.args.get('index', '-100')
    sex = request.args.get('sex', '-100')
    pageNo = request.args.get('pageNo', '1')
    response = requests.get('http://121.40.228.127:3300/singer/list', params={
        'area': area,
        'genre': genre,
        'index': index,
        'sex': sex,
        'pageNo': pageNo
    })
    return jsonify(response.json())

@app.route('/album', methods=['GET'])
def get_album_info():
    albummid = request.args.get('albummid')
    if not albummid:
        return jsonify({'error': 'No albummid provided'}), 400
    response = requests.get(f'http://121.40.228.127:3300/album?albummid={albummid}')
    return jsonify(response.json())


@app.route('/album/songs', methods=['GET'])
def get_album_songs():
    albummid = request.args.get('albummid')
    if not albummid:
        return jsonify({'error': 'No albummid provided'}), 400
    response = requests.get(f'http://121.40.228.127:3300/album/songs?albummid={albummid}')
    return jsonify(response.json())

@app.route('/comment', methods=['GET'])
def get_comments():
    comment_id = request.args.get('id')
    page_no = request.args.get('pageNo', '1')
    page_size = request.args.get('pageSize', '20')
    comment_type = request.args.get('type', '0')
    biztype = request.args.get('biztype')
    response = requests.get(f'http://121.40.228.127:3300/comment', params={'id': comment_id, 'pageNo': page_no, 'pageSize': page_size, 'type': comment_type, 'biztype': biztype})
    return jsonify(response.json())


@app.route('/comment/send', methods=['POST'])
def send_comment():
    comment_id = request.json.get('id')
    biztype = request.json.get('biztype')
    content = request.json.get('content')
    response = requests.post('http://121.40.228.127:3300/comment/send', json={'id': comment_id, 'biztype': biztype, 'content': content})
    return jsonify(response.json())

@app.route('/comment/del', methods=['POST'])
def delete_comment():
    comment_id = request.json.get('id')
    response = requests.post('http://121.40.228.127:3300/comment/del', json={'id': comment_id})
    return jsonify(response.json())

@app.route('/comment/like', methods=['POST'])
def like_comment():
    comment_id = request.json.get('id')
    like_type = request.json.get('type', '1')
    response = requests.post('http://121.40.228.127:3300/comment/like', json={'id': comment_id, 'type': like_type})
    return jsonify(response.json())
@app.route('/radio/category', methods=['GET'])
def get_radio_category():
    response = requests.get('http://121.40.228.127:3300/radio/category')
    return jsonify(response.json())


@app.route('/radio', methods=['GET'])
def get_radio_songs():
    radio_id = request.args.get('id')
    response = requests.get(f'http://121.40.228.127:3300/radio?id={radio_id}')
    return jsonify(response.json())
@app.route('/mv', methods=['GET'])
def get_mv_info():
    vid = request.args.get('id')
    if not vid:
        return jsonify({'error': 'No video ID provided'}), 400
    response = requests.get(f'http://121.40.228.127:3300/mv?id={vid}')
    return jsonify(response.json())

@app.route('/mv/url', methods=['GET'])
def get_mv_url():
    vid = request.args.get('id')
    response = requests.get(f'http://121.40.228.127:3300/mv/url?id={vid}')
    return jsonify(response.json())

@app.route('/mv/category', methods=['GET'])
def get_mv_category():
    response = requests.get('http://121.40.228.127:3300/mv/category')
    return jsonify(response.json())

@app.route('/mv/list', methods=['GET'])
def get_mv_list():
    page_no = request.args.get('pageNo', '1')
    page_size = request.args.get('pageSize', '20')
    area = request.args.get('area', '15')
    version = request.args.get('version', '7')
    response = requests.get(f'http://121.40.228.127:3300/mv/list', params={'pageNo': page_no, 'pageSize': page_size, 'area': area, 'version': version})
    return jsonify(response.json())

@app.route('/mv/like', methods=['POST'])
def like_mv():
    vid = request.json.get('id')
    type_like = request.json.get('type', '1')
    response = requests.post('http://121.40.228.127:3300/mv/like', json={'id': vid, 'type': type_like})
    return jsonify(response.json())


@app.route('/top/category', methods=['GET'])
def get_top_category():
    show_detail = request.args.get('showDetail', '0')
    response = requests.get(f'http://121.40.228.127:3300/top/category', params={'showDetail': show_detail})
    return jsonify(response.json())

@app.route('/top', methods=['GET'])
def get_top_details():
    top_id = request.args.get('id', '4')
    page_size = request.args.get('pageSize', '100')
    period = request.args.get('period')
    time = request.args.get('time')
    params = {'id': top_id, 'pageSize': page_size}
    if period:
        params['period'] = period
    if time:
        params['time'] = time
    response = requests.get(f'http://121.40.228.127:3300/top', params=params)
    return jsonify(response.json())

@app.route('/user/follow/singers', methods=['GET'])
def get_followed_singers():
    page_no = request.args.get('pageNo', '1')
    page_size = request.args.get('pageSize', '20')
    user_id = request.args.get('id')
    params = {'pageNo': page_no, 'pageSize': page_size}
    if user_id:
        params['id'] = user_id
    response = requests.get('http://121.40.228.127:3300/user/follow/singers', params=params)
    return jsonify(response.json())


@app.route('/user/follow/users', methods=['GET'])
def get_followed_users():
    page_no = request.args.get('pageNo', '1')
    page_size = request.args.get('pageSize', '20')
    user_id = request.args.get('id')
    params = {'pageNo': page_no, 'pageSize': page_size}
    if user_id:
        params['id'] = user_id
    response = requests.get('http://121.40.228.127:3300/user/follow/users', params=params)
    return jsonify(response.json())

@app.route('/user/fans', methods=['GET'])
def get_user_fans():
    page_no = request.args.get('pageNo', '1')
    page_size = request.args.get('pageSize', '20')
    user_id = request.args.get('id')
    params = {'pageNo': page_no, 'pageSize': page_size}
    if user_id:
        params['id'] = user_id
    response = requests.get('http://121.40.228.127:3300/user/fans', params=params)
    return jsonify(response.json())


@app.route('/user/follow', methods=['POST'])
def follow_singer():
    singermid = request.json.get('singermid')
    operation = request.json.get('operation', '1')
    if not singermid:
        return jsonify({'error': 'No singermid provided'}), 400
    response = requests.post('http://121.40.228.127:3300/user/follow', json={'singermid': singermid, 'operation': operation})
    return jsonify(response.json())

if __name__ == '__main__':
    app.run(debug=True)

